package com.hlk.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-01-15 9:56
 */
@Configuration
public class RabbitmqConfig {

    public static final String EXCHANGE_ARTICLE = "exchange_article";
    public static final String QUEUE_DOWNLOAD_HTML = "queue_download_html";
    // 定义交换机的名字
    public static final String EXCHANGE_DELAY = "exchange_delay";

    // 定义队列的名字
    public static final String QUEUE_DELAY = "queue_delay";


    @Bean(EXCHANGE_ARTICLE)
    public Exchange exchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_ARTICLE)
                .durable(true)
                .build();
    }

    @Bean(QUEUE_DOWNLOAD_HTML)
    public Queue queue() {
        return new Queue(QUEUE_DOWNLOAD_HTML);
    }

    @Bean
    public Binding binding(@Qualifier(EXCHANGE_ARTICLE) Exchange exchange, @Qualifier(QUEUE_DOWNLOAD_HTML) Queue queue) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("article.#")
                .noargs();
    }

    // 创建交换机
    @Bean(EXCHANGE_DELAY)
    public Exchange delayExchange() {
        return ExchangeBuilder
                .topicExchange(EXCHANGE_DELAY)
                .delayed()          // 开启支持延迟消息
                .durable(true)
                .build();
    }

    // 创建队列
    @Bean(QUEUE_DELAY)
    public Queue delayQueue() {
        return new Queue(QUEUE_DELAY);
    }

    // 队列绑定交换机
    @Bean
    public Binding delayBinding(
            @Qualifier(QUEUE_DELAY) Queue queue,
            @Qualifier(EXCHANGE_DELAY) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("publish.delay.#")
                .noargs();      // 执行绑定
    }
}
